package lib.popup

import android.content.Context
import android.text.TextUtils
import android.text.method.LinkMovementMethod
import android.view.LayoutInflater
import android.view.accessibility.AccessibilityEvent
import androidx.databinding.DataBindingUtil
import com.lxj.xpopup.XPopup
import com.lxj.xpopup.core.BasePopupView
import com.lxj.xpopup.core.CenterPopupView
import com.lxj.xpopup.enums.PopupAnimation
import com.lxj.xpopup.interfaces.SimpleCallback
import lib.popup.databinding.MviPopupMessageBinding

/**
 * author: luoXiong
 * e-mail: 382060748@qq.com
 * date: 2020/10/27 16:58
 * version: 1.0
 * desc:
 */
class _MessagePopupView(context: Context) : CenterPopupView(context) {

    var mTitle: String? = null
    var mContent: String? = null
    var mContentGravity: Int = 0
    var mContentColorInt: Int? = null



    var mBtnText: String? = null
    var mBtnColorInt: Int? = null
    var mBtnClickListener: (() -> Unit)? = null

    var mOnDismissListener: (() -> Unit)? = null

    private var binding: MviPopupMessageBinding? = null

    fun show(info: PopupCommonInfo) {
        if (TextUtils.isEmpty(mBtnText)) {
            mBtnText = "我知道了"
        }
        var pwDismissCb: SimpleCallback? = null
        if (mOnDismissListener != null) {
            pwDismissCb = object : SimpleCallback() {
                override fun onDismiss(popupView: BasePopupView) {
                    mOnDismissListener!!.invoke()
                }

                // 如果你自己想拦截返回按键事件，则重写这个方法，返回true即可
                override fun onBackPressed(popupView: BasePopupView): Boolean {
                    return true
                }
            }
        }
        val builder = XPopup.Builder(context)
            .isDestroyOnDismiss(info.isDestroyOnDismiss)
            .dismissOnTouchOutside(info.isDismissOnTouchOutside)
            .dismissOnBackPressed(info.isDismissOnBackPressed)
            .popupAnimation(PopupAnimation.NoAnimation)
            .setPopupCallback(pwDismissCb)
        builder.asCustom(this).show()
    }

    override fun addInnerContent() {
        binding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout._mvi_popup_message, centerPopupContainer, true)
        val tvTitle = binding!!.tvTitle
        val tvContent = binding!!.tvContent
        val tvConfirm = binding!!.tvConfirm
        tvTitle.text = mTitle
        // 设置内容可以滑动
        tvContent.setMovementMethod(LinkMovementMethod.getInstance())
        tvContent.text = mContent
        if (mContentColorInt != null && mContentColorInt != 0) {
            tvContent.setTextColor(mContentColorInt!!)
        }
        if (mContentGravity > 0) {
            tvContent.gravity = mContentGravity
        }
        // 按钮
        tvConfirm.text = mBtnText.orEmpty()
        if (mBtnColorInt != null && mBtnColorInt != 0) {
            tvConfirm.setTextColor(mBtnColorInt!!)
        }
        tvConfirm.setOnClickListener {
            dismiss()
            mBtnClickListener?.invoke()
        }

    }

    override fun getPopupHeight(): Int {
        return super.getPopupHeight()
    }

    override fun getMaxHeight(): Int {
        return super.getMaxHeight()
        //  return (ScreenUtils.getScreenHeight() * 0.85).toInt()  // 加上后,如果内容高度有5行,有时候也只显示4行,剩下的要滑动,体验不好
    }

}